﻿<UserControl x:Class="Collab.Modules.ContactBrowser.View.ContactsView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:vm="clr-namespace:Collab.Modules.ContactBrowser.ViewModel"
         xmlns:view="clr-namespace:Collab.Modules.ContactBrowser.View"
         mc:Ignorable="d" 
         d:DesignHeight="400" d:DesignWidth="288" HorizontalAlignment="Stretch">

    <UserControl.Resources>

        <LinearGradientBrush x:Key="InactiveHeading" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF4D6082" Offset="0" />
            <GradientStop Color="#FF3D5277" Offset="0.3" />
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="ActiveHeading" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FFFFFCF4" Offset="0" />
            <GradientStop Color="#FFFFF3CD" Offset="0.49" />
            <GradientStop Color="#FFFFE8A6" Offset="0.50" />
            <GradientStop Color="#FFFFE8A6" Offset="1" />
        </LinearGradientBrush>

        <Style x:Key="TreeViewItemFocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <PathGeometry x:Key="TreeArrow" Figures="M0,0 L0,6 L6,0 z"/>
        <Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="Width" Value="16"/>
            <Setter Property="Height" Value="16"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border Width="16" Height="16" Background="Transparent" Padding="5,5,5,5">
                            <Path x:Name="ExpandPath" Fill="Transparent" Stroke="#FF989898" Data="{StaticResource TreeArrow}">
                                <Path.RenderTransform>
                                    <RotateTransform Angle="135" CenterX="3" CenterY="3"/>
                                </Path.RenderTransform>
                            </Path>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF1BBBFA"/>
                                <Setter Property="Fill" TargetName="ExpandPath" Value="Transparent"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="RenderTransform" TargetName="ExpandPath">
                                    <Setter.Value>
                                        <RotateTransform Angle="180" CenterX="3" CenterY="3"/>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="Fill" TargetName="ExpandPath" Value="#FF595959"/>
                                <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF262626"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
            <Setter Property="AllowDrop" Value="True" />
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="Padding" Value="1,0,0,0"/>
            <Setter Property="Foreground" Value="#FF333333"/>
            <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <Grid HorizontalAlignment="Stretch">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>

                            <Border x:Name="Bd" HorizontalAlignment="Stretch" SnapsToDevicePixels="true" Grid.Column="0" Padding="0,1,0,1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                                <StackPanel Orientation="Horizontal">
                                    <ToggleButton x:Name="Expander" Style="{StaticResource ExpandCollapseToggleStyle}" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"/>
                                    <ContentPresenter x:Name="PART_Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header"/>
                                </StackPanel>
                            </Border>
                            <ItemsPresenter x:Name="ItemsHost" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <!--<Trigger Property="IsExpanded" Value="false">
                                <Setter Property="Background" TargetName="Bd" Value="{StaticResource ActiveHeading}"/>
                                <Setter Property="Foreground" Value="#FF000000"/>
                            </Trigger>-->
                            <Trigger Property="IsExpanded" Value="false">
                                <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="HasItems" Value="false">
                                <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
                            </Trigger>
                            <!--<Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" TargetName="Bd" Value="{StaticResource ActiveHeading}"/>
                                <Setter Property="Foreground" Value="#FF000000"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="true"/>
                                    <Condition Property="IsSelectionActive" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="{StaticResource ActiveHeading}"/>
                                <Setter Property="Foreground" Value="#FF000000"/>
                            </MultiTrigger>-->
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#FF000000"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">
                    <Setter Property="ItemsPanel">
                        <Setter.Value>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel/>
                            </ItemsPanelTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

        <DataTemplate DataType="{x:Type vm:SearchContactViewModel}">
            <view:SearchContactView/>
        </DataTemplate>




    </UserControl.Resources>

    <Grid Margin="5" HorizontalAlignment="Stretch">
        <StackPanel>
            <Expander IsExpanded="False" ExpandDirection="Down" Header="Search">
                <ContentControl x:Name="SearchContent" Content="{Binding Path=SearchViewModel}" />
            </Expander>

            <TreeView x:Name="GroupTreeView" BorderThickness="0" HorizontalAlignment="Stretch" ItemsSource="{Binding Groups}" 
                      ItemContainerStyle="{StaticResource TreeViewItemStyle}"
                      MouseDown="GroupTreeView_MouseDown"
                      MouseMove="GroupTreeView_MouseMove"
                      DragEnter="GroupTreeView_CheckDropTarget"
                      DragLeave="GroupTreeView_DragLeave"
                      DragOver="GroupTreeView_CheckDropTarget"
                      Drop="GroupTreeView_Drop">
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Users}" >
                        <TextBlock Text="{Binding GroupName}"/>


                        <HierarchicalDataTemplate.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <!--<Ellipse x:Name="OnlineStatus" Fill="#FFCBCBCB" HorizontalAlignment="Left" Margin="8,2,0,0" VerticalAlignment="Top" Width="7.5" Height="7.5" d:LayoutOverrides="VerticalAlignment"/>-->
                                    <Label Margin="2,0,8,0" VerticalAlignment="Top" Content="{Binding UserName, Mode=Default}">
                                        <Label.ContextMenu>
                                            <ContextMenu>
                                                <MenuItem x:Name="DeleteContact" Header="Delete Contact..." Click="DeleteContact_Click" />
                                                <MenuItem x:Name="InviteToGroup" Header="Invite To Group..."/>
                                                <MenuItem x:Name="InviteToSession" Header="Invite To Session..." Click="InviteToSession_Click" />
                                                <MenuItem x:Name="Join" Header="Join Existing Session..." Click="Join_Click" />
                                            </ContextMenu>
                                        </Label.ContextMenu>
                                    </Label>
                                </StackPanel>
                            </DataTemplate>
                        </HierarchicalDataTemplate.ItemTemplate>

                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>

        </StackPanel>
    </Grid>
</UserControl>

    